﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.DisplayManagement.Descriptors;
using Orchard.UI.Zones;
using Orchard.DisplayManagement;
using Orchard.ContentManagement;
using System.IO;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard;
using Orchard.Environment.Extensions;
using Orchard.Environment;
using System.Web.Mvc;

namespace SchoolPointe.Menus
{
    public class Shapes : IShapeTableProvider
    {   
        
        private  Work<IEnumerable<IContentPartDriver>> _drivers;
        private readonly Work<IContentManager> _contentManger;
        private  Work<IShapeFactory> _shapeFactory;

        public Shapes(Work<IShapeFactory> shapeFactory, Work<IEnumerable<IContentPartDriver>> drivers, Work<IContentManager> contentManger )
        {
            _drivers = drivers;
            _contentManger = contentManger;
            _shapeFactory = shapeFactory;
        }

        public void Discover(ShapeTableBuilder builder)
        {
            builder.Describe("Menu_Editor")
                .OnCreating(creating => creating.Behaviors.Add(new ZoneHoldingBehavior(() => creating.New.Zone())));
            builder.Describe("NestedEditor")
                .OnCreating(creating => creating.Behaviors.Add(new ZoneHoldingBehavior(() => creating.New.Zone())))
                .OnCreated(created => {
                    var editor = created.Shape;
                    editor.Child.Add(created.New.PlaceChildContent(Source: editor));
                })
                .OnDisplaying(displaying => {
                    var shape = displaying.Shape;
                    var contentItem = shape.Editor.ContentItem;

                    string contentTypeName = contentItem.TypeDefinition.Name;
                    string displayType = shape.DisplayType;
                    displaying.ShapeMetadata.Wrappers.Add("Editor_Wrapper__" + contentTypeName);

                    if (!string.IsNullOrEmpty(displayType))
                        displaying.ShapeMetadata.Wrappers.Add("Editor_Wrapper__" + contentTypeName + "__" + displayType);                   
                });
        }

        [Shape]
        public void NestedEditor(dynamic Display, dynamic Shape, HtmlHelper Html, TextWriter Output, dynamic Editor, string ParentPrefix, string DisplayType)
        {            
            Shape.ContentItem = Editor.ContentItem;

            if (!string.IsNullOrEmpty(DisplayType))
                Shape.Metadata.DisplayType = DisplayType;

            var oldPrefix = Html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix;
            try
            {
                if (ParentPrefix != null)
                    Html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = ParentPrefix;

                Output.Write(Display(Editor));
            }
            finally
            {
                Html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = oldPrefix;
            }           
        }
    }
}